<? 
require("adm_header.php"); 
writeCommonAdminTable(LA_ADMIN_REGIONS,1);
$tplRegion = new tplLoad();
$tplRegion->assign("set_region","$set_region");

$lSelected=getParam("catid","");

$lTextBoxValue=getParam("region_name","");

if (getParam("edit_change",""))
{
	$lNewFatherId=getParam("edit_region_catid","");
	$lNewName=getParam("edit_region_name","");
	$lEditRegionId=getParam("edit_region","");
	$lEditRegionPri=getParam("edit_region_pri","");
	
	$lSql="update $reg_tbl set reg_name='" . strToDb($lNewName) . "',reg_father='" . strToDb($lNewFatherId) . "',reg_pri='$lEditRegionPri' where reg_id=$lEditRegionId";
	q($lSql);
	$lSelected=$lNewFatherId;
	
}
if (getParam("edit",""))
{
	$lEditRegion=$lSelected;
	
	$lSql="select * from $reg_tbl where reg_id=$lEditRegion";
	$lRes=q($lSql);
	$lRow=mysql_fetch_array($lRes);
	$lEditRegionName=$lRow["reg_name"];
	$lSelected=$lRow["reg_father"];
	$lEditRegionPri=$lRow["reg_pri"];
	$tplRegion->assign("edit_region_pri","$lEditRegionPri");
	$tplRegion->assign("edit_region","$lEditRegion");
	$tplRegion->assign("edit_region_name","$lEditRegionName");
	
}
if (getParam("add",""))
{
	$lSql="insert into $reg_tbl (reg_name,reg_father) values ('" . $lTextBoxValue . "','$lSelected')";
	q($lSql);
}
if (getParam("del",""))
{
	if (getParam("confirm","")=="")
		$tplRegion->assign("del","$lSelected");
	else if (getParam("del","") && getParam("confirm",""))
	{
		$lSql="delete from $reg_tbl where reg_id=$lSelected";
		q($lSql);
		$tplRegion->assign("deleted","$lSelected");
	}
}



$tplRegion->assign("options",getRegionTree(0,0,$lSelected));
$lRegOptions="<option value=''> -- ".LA_ADMIN_REGIONS_SELECT." -- </option>";
$lRegOptions.=getRegionsForOneLevel(0,getParam("reg_1",""),"");
$tplRegion->assign("reg_1_options",$lRegOptions);



if (getParam("import",""))
{
	$c=0;
	$ignored=0;
	
	if ($_FILES['userfile']['type']=="text/plain")
	{
		$fp=fopen($_FILES['userfile']['tmp_name'],"r");
		$lFileContent=fread($fp,filesize($_FILES['userfile']['tmp_name']));
		$lSplitted=preg_split("/\r?\n|\r/", $lFileContent);
		
		foreach ($lSplitted as $lItem)
		{
			//$lItem=htmlspecialchars($lItem,ENT_QUOTES);
			$lItemArray=split("\/",$lItem);
			
			$lLevelOne=strToDb(utf8_encode($lItemArray[0]));
			$lLevelTwo=strToDb(utf8_encode($lItemArray[1]));
			$lLevelThree=strToDb(utf8_encode($lItemArray[2]));
			
			// First level
			$lCheck=q("select reg_id from $reg_tbl where reg_name='".trim($lLevelOne)."' AND reg_father=0");
			if (mysql_num_rows($lCheck)==0)
			{
				if (trim($lLevelOne)!="")
				{
				$lRes=q("insert into $reg_tbl (reg_name,reg_father) values ('$lLevelOne',0)");
				$lFatherId=mysql_insert_id();	
				$c++;			
				}
			}
			else 
			{
				$lRow=mysql_fetch_array($lCheck);	
				$lFatherId=$lRow["reg_id"];
			}
			
			// Second level
			$lCheck=q("select reg_id from $reg_tbl where reg_name='".trim($lLevelTwo)."' AND reg_father=$lFatherId");
			if (mysql_num_rows($lCheck)==0)
			{
				if (trim($lLevelTwo)!="")
				{
					$lRes=q("insert into $reg_tbl (reg_name,reg_father) values ('$lLevelTwo',$lFatherId)");
					$lFatherId=mysql_insert_id();	
					$c++;						
				}
			}
			else 
			{
				$lRow=mysql_fetch_array($lCheck);	
				$lFatherId=$lRow["reg_id"];
			}
			
			// Second level
			$lCheck=q("select reg_id from $reg_tbl where reg_name='".trim($lLevelThree)."' AND reg_father=$lFatherId");
			if (mysql_num_rows($lCheck)==0)
			{
				if (trim($lLevelThree)!="")
				{
				$lRes=q("insert into $reg_tbl (reg_name,reg_father) values ('$lLevelThree',$lFatherId)");
				$lFatherId=mysql_insert_id();	
				$c++;			
				}
			}
			else 
			{
				$lRow=mysql_fetch_array($lCheck);	
				$lFatherId=$lRow["reg_id"];
			}
			
			
		
		}
	}
	echo "<p><b>$c</b> ".LA_ADMIN_REGIONS_NUM_IMPORTED."</p>";
}

$tplRegion->display("adm_regions.tpl");
writeCommonAdminTable("","");
require("adm_footer.php"); 
?>